﻿using System;
using System.Web.UI.WebControls;
using Core.Library;
using DataObject.Property;
using DataObject.PropertyValue;

namespace KMT.Controls.Common
{
    public partial class Sidebar : System.Web.UI.UserControl
    {
        public int PropertyId = 1; // Sơ mi

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                ListPropertyByType(PropertyId);
        }

        private void ListPropertyByType(int propertyId)
        {
            var lstProperty = PropertyController.Property_GetByParentId(propertyId);
            if (lstProperty.Count > 0)
            {
                rptProperty.DataSource = lstProperty;
                rptProperty.DataBind();
            }            
        }

        protected void rptProperty_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                var rptPropertyValue = (Repeater) e.Item.FindControl("rptPropertyValue");
                var ltScript         = (Literal)e.Item.FindControl("ltScript");
                var propertyId = ConvertUtility.ToInt32(((HiddenField)e.Item.FindControl("hfPropertyId")).Value);
                if (propertyId > 0)
                {
                    var lstPropertyValue = PropertyValueController.PropertyValue_GetByPropertyId(propertyId);
                    if (lstPropertyValue.Count > 0)
                    {                       
                        var script          = string.Empty;
                                               
                        script += "<script type=\"text/javascript\">";
                        script += "jQuery('#filter-label" + propertyId + "').each(function () {";
                        script += "jQuery(this).addClass('active');";
                        script += "jQuery(this).toggle(function () {";
                        script += "jQuery(this).removeClass('active').next().slideUp(200);";
                        script += "}, function () {";
                        script += "jQuery(this).addClass('active').next().slideDown(200);";
                        script += "});";
                        script += "});";

                        script += "decorateDataList('narrow-by-list');";
                        script += "</script>";

                        ltScript.Text = script;

                        rptPropertyValue.DataSource = lstPropertyValue;
                        rptPropertyValue.DataBind();
                    }                    
                }                
            }
        }
    }
}